// pages/newAddress/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    username: '',
    phone:'',
    address:'',
    detailAddress:'',
    checked: false,
    imageUrl:'http://feishoukeji.com:8080/Recycle/static/image/',
    areaList: [], //小区列表
    currentArea: [], //小区备份
    pageNum: 1,
    community_id:'',
    address_id:'',
    nomore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllareaList()
    if (!options.id) { wx.removeStorageSync('editAddress'); return} ;
    const editaddress = wx.getStorageSync('editAddress')
    let isdefault = editaddress.isdefault == 1 ? true : false
    this.setData({
      username: editaddress.addr_name,
      phone: editaddress.addr_phone,
      detailAddress: editaddress.addr_detail,
      checked: isdefault,
      address: editaddress.community_name,
      community_id: editaddress.community_id,
      address_id: options.id
    })
  },
  userInput(e){
    this.setData({
      username: e.detail.value
    })
  },
  phoneInput(e){
    this.setData({
      phone: e.detail.value
    })
  },

  detailInput(e){
    this.setData({
      detailAddress: e.detail.value
    })
  },
  switch1Change(e){
    this.setData({
      checked: e.detail
    })
  },
  //提交表单
  async formSubmit(e) {
    const {address_id} = this.data
    const { checked, community_id} = this.data
    let username = e.detail.value.username;
    let phone = e.detail.value.phone;
    let dong = e.detail.value.dong;
    let danyuan = e.detail.value.danyuan;
    let menpai = e.detail.value.menpai;
    let detailAddress = '';
    if(dong){
      dong = dong+'栋'
   }
    if(danyuan){
      danyuan = danyuan+ '单元'
    }
    if(menpai){
      menpai =menpai+'号'
   }
    detailAddress = dong + danyuan + menpai;
    if(!detailAddress){
      Toast('详细地址不能为空'); return
    }
    let isdefault = checked ? 1 : 0
    let rightPhone = /^1\d{10}$/.test(phone)
    if(!username){Toast('请输入收件人'); return}
    if (!rightPhone){Toast('电话号码不正确'); return}
      if (address_id){
        let params = {
          token: wx.getStorageSync('token'),
          isdefault,
          name: username,
          phone,
          community_id,
          address_id
        };
        detailAddress ? params.detail = detailAddress : '';
        const res = await api('POST', 'Recycle/address/updateAddress',params)
        if (res.data.code === 200) {
          Toast(res.data.msg)
          wx.navigateBack({
            delta: 1
          })
        }
      }else{
        let params = {
          token: wx.getStorageSync('token'),
          isdefault,
          name: username,
          phone,
          community_id,
          address_id
        };
        detailAddress ? params.detail = detailAddress : '';
        const res = await api('POST', 'Recycle/address/insertAddr', {
          token: wx.getStorageSync('token'),
          isdefault,
          name: username,
          phone,
          community_id,
          detail: detailAddress
        })
        if (res.data.code === 200) {
          Toast(res.data.msg)
          wx.navigateBack({
            delta: 1
          })
        }
      }
  },
  //关闭面板
  onClose(){
    this.setData({
      show: false
    })
  },
  confirmSelect(e){
    console.log(e)
    let ads = ''
    e.detail.values.forEach((item) => {
      if(item){
        ads += item.name
      }   
    })
    this.setData({
      address: ads,
      show: false
    })
    
  },
  areaShow(){
    this.setData({
      show:true
    })
  },
  async getAllareaList(){
    let { pageNum, areaList} = this.data
    const res = await api('GET','Recycle/area/getCommunities',{
      pageNum
    })
    if(res.data.code===200){
      if(res.data.data.length<10){
        this.setData({
          nomore: false
        })
      }
      this.setData({
        areaList: areaList.concat(res.data.data),
      })
    }
  },
  chooseArea(e){
    let id = e.currentTarget.dataset.id
    this.data.areaList.forEach(i => {
      if (id == i.community_id){
        this.setData({
          address: i.community_name,
          show:false,
          community_id:id
        })
      }
    })
  },
  loadMore(){
    const { pageNum} = this.data
    this.setData({
      pageNum: pageNum+1
    })
    this.getAllareaList()
  },
  async searchArea(e) {
    const value = e.detail
    const res = await api('GET', 'Recycle/area/getCommunities', {
      pageNum:1,
      key: value
    })
    if (res.data.code === 200) {
      if (res.data.data && res.data.data.length < 10) {
        this.setData({
          nomore: false
        })
      }
      this.setData({
        areaList: res.data.data,
      })
    }
  },
  conso(){
    console.log('123')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.conso()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})